package com.sise.parkinglot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sise.parkinglot.bean.CodeMsg;
import com.sise.parkinglot.entity.Charger;
import com.sise.parkinglot.mapper.ChargerMapper;
import com.sise.parkinglot.service.IChargerService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author ZJ
 */
@Service
public class ChargerServiceImpl extends ServiceImpl<ChargerMapper, Charger> implements IChargerService {

    @Resource
    private ChargerMapper chargerMapper;

    @Resource
    private IChargerService chargerService;

    @Override
    public CodeMsg<List<Charger>> queryChargerList(Map<String, Object> map) {
        if (map.isEmpty()) {
            return new CodeMsg<List<Charger>>().setCode(400).setMsg("参数缺失");
        }
        Page<Charger> page = new Page<>((int) map.get("current"), (int) map.get("pageSize"));
        QueryWrapper<Charger> chargerQueryWrapper = new QueryWrapper<>();
        if (map.get("id") != null && !map.get("id").equals("")) {
            chargerQueryWrapper.eq("id", map.get("id"));
        }
        if (map.get("cardType") != null && !map.get("cardType").equals("")) {
            chargerQueryWrapper.eq("card_type", map.get("cardType"));
        }
        if (map.get("charge") != null && !map.get("charge").equals("")) {
            chargerQueryWrapper.like("charge", map.get("charge"));
        }
        if (map.get("stationType") != null && !map.get("stationType").equals("")) {
            chargerQueryWrapper.eq("station_type", map.get("stationType"));
        }
        if (map.get("state") != null && !map.get("state").equals("")) {
            chargerQueryWrapper.eq("state", map.get("state"));
        }
        chargerQueryWrapper.lambda().orderByAsc(Charger::getStationType).orderByDesc(Charger::getCardType);
        IPage<Charger> chargerPage = chargerMapper.selectPage(page, chargerQueryWrapper);
        List<Charger> chargerList = chargerPage.getRecords();
        return new CodeMsg<List<Charger>>().setCode(200).setMsg(String.valueOf(chargerPage.getTotal())).setData(chargerList);
    }

    @Override
    public CodeMsg<String> addCharger(Charger charger) {
        QueryWrapper<Charger> chargerQueryWrapper = new QueryWrapper<>();
        chargerQueryWrapper.lambda().eq(Charger::getCardType, charger.getCardType())
                .eq(Charger::getStationType, charger.getStationType());
        Charger getCharger = chargerMapper.selectOne(chargerQueryWrapper);
        if (getCharger != null) {
            return new CodeMsg<String>().setCode(400).setMsg("收费标准已存在，添加失败");
        }
        if (charger.getId() != null) {
            charger.setId(null);
        }
        int insert = chargerMapper.insert(charger);
        if (insert == 1) {
            return new CodeMsg<String>().setCode(200).setMsg("添加成功");
        }
        return new CodeMsg<String>().setCode(401).setMsg("添加失败");
    }

    @Override
    public CodeMsg<String> removeCharger(int[] ids) {
        List<Integer> chargerList = new ArrayList<>();
        for (int id : ids) {
            chargerList.add(id);
        }
        if (chargerService.removeByIds(chargerList)) {
            return new CodeMsg<String>().setCode(200).setMsg("删除成功");
        }
        return new CodeMsg<String>().setCode(400).setMsg("删除失败");
    }

    @Override
    public CodeMsg<String> updateState(int[] ids, int state) {
        List<Charger> chargerList = new ArrayList<>();
        for (int id : ids) {
            chargerList.add(new Charger().setId(id).setState(state));
        }
        if (chargerService.updateBatchById(chargerList)) {
            return new CodeMsg<String>().setCode(200).setMsg("操作成功");
        }
        return new CodeMsg<String>().setCode(400).setMsg("操作失败");
    }

    @Override
    public CodeMsg<String> updateCharger(Charger charger) {
        int update = chargerMapper.updateById(charger);
        if (update == 1) {
            return new CodeMsg<String>().setCode(200).setMsg("编辑成功");
        }
        return new CodeMsg<String>().setCode(401).setMsg("编辑失败");
    }

    @Override
    public CodeMsg<List<Charger>> getChargerList() {
        QueryWrapper<Charger> chargerQueryWrapper = new QueryWrapper<>();
        chargerQueryWrapper.lambda().eq(Charger::getState, 1).orderByAsc(Charger::getStationType).orderByDesc(Charger::getCardType);
        return new CodeMsg<List<Charger>>().setCode(200).setMsg("获取成功").setData(chargerMapper.selectList(chargerQueryWrapper));
    }
}
